package com.mall.controller;

import com.mall.dto.CouponCreateDto;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsCoupon;
import com.mall.service.CouponService;
import com.mall.utils.Result;
import com.mall.vo.CouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult<SmsCoupon>> list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                              @RequestParam(required = false) String name,
                                              @RequestParam(required = false) Integer type) {
        PageResult<SmsCoupon> result = couponService.list(pageNum, pageSize, name, type);
        return Result.success(result, "操作成功");
    }

    /**
     * 添加优惠券
     *
     * @param couponParam
     * @return
     */
    @PostMapping("/create")
    public Result<Integer> create(@RequestBody CouponCreateDto couponParam) {
        Integer count = couponService.create(couponParam);
        return Result.success(count, "操作成功");
    }

    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result<Integer> delete(@PathVariable Integer id) {
        Integer count = couponService.delete(id);
        return Result.success(count, "操作成功");
    }

    /**
     * 获取单个优惠券的详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<CouponVo> couponMsg(@PathVariable Integer id) {
        CouponVo vo = couponService.couponMsg(id);
        return Result.success(vo, "操作成功");
    }

    /**
     * 修改优惠券
     *
     * @param couponParam
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<Integer> update(@PathVariable Integer id, @RequestBody CouponCreateDto couponParam) {
        Integer count = couponService.update(id, couponParam);
        return Result.success(count, "操作成功");
    }

}
